package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.UserInfo;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.utils.IpUtil;
import com.cq.hd.member.api.TbAgentEnterApplyApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.AgentBankCardVo;
import com.cq.hd.member.api.vo.AgentEnterApplyStateVo;
import com.cq.hd.member.api.vo.AgentEnterApplyVo;
import com.cq.hd.member.api.vo.PreAgentApplyMerchantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/agentEnterApply")
@Api(tags = "经纪人入驻相关接口")
public class AgentEnterApplyController {

    @Autowired
    private TbAgentEnterApplyApi agentEnterApplyApi;

    @ApiOperation(value = "经纪人入驻预申请（供用户点击入驻申请链接调用）")
    @ApiResponse(response = PreAgentApplyMerchantVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/preApply")
    public Result<PreAgentApplyMerchantVo> preApply(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                                    @RequestBody PreAgentEnterApplyDto preAgentApplyMerchantDto) {
        preAgentApplyMerchantDto.setUserId(appLoginUser.getUserId());
        return agentEnterApplyApi.preApply(preAgentApplyMerchantDto);
    }

    @ApiOperation(value = "经纪人绑定商家（经纪人开户）")
    @PostMapping("/bind")
    public Result<Boolean> bind(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                @RequestBody BusinessAgentBindDto businessAgentBindDto, HttpServletRequest request) {
        businessAgentBindDto.setUserId(appLoginUser.getUserId());
        businessAgentBindDto.setRequestIp(IpUtil.getIpAddr(request));
        return agentEnterApplyApi.bind(businessAgentBindDto);
    }

    @ApiOperation(value = "经纪人开户")
    @PostMapping("/bindHf")
    public Result<AgentEnterApplyVo> bindHf(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                @RequestBody AgentBindHfDto agentBindHfDto, HttpServletRequest request) {
        agentBindHfDto.setUserId(appLoginUser.getUserId());
        agentBindHfDto.setRequestIp(IpUtil.getIpAddr(request));
        return agentEnterApplyApi.bindHf(agentBindHfDto);
    }

    @ApiOperation(value = "返回已签约的银行卡列表")
    @ApiResponse(response = AgentBankCardVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/listBank")
    public Result<List<AgentBankCardVo>> listBank(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        return agentEnterApplyApi.listBank(appLoginUser.getUserId());
    }

    @ApiOperation(value = "经纪人入驻，绑定银行卡，并做签约，返回签约地址")
    @PostMapping("/enterApply")
    public Result<AgentEnterApplyVo> enterApply(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                                @RequestBody AgentEnterApplyDto agentEnterApplyDto,
                                                HttpServletRequest request) {
        agentEnterApplyDto.setUserId(appLoginUser.getUserId());
        agentEnterApplyDto.setApplyIp(IpUtil.getIpAddr(request));
        return agentEnterApplyApi.enterApply(agentEnterApplyDto);
    }

    // 根据入驻状态判断是否需要重新提交
    @ApiOperation(value = "获取入驻状态")
    @GetMapping("/getEnterInfoState")
    public Result<AgentEnterApplyStateVo> getEnterInfoState(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        return agentEnterApplyApi.getEnterInfoState(appLoginUser.getUserId());
    }

    @ApiOperation(value = "修改银行卡")
    @PutMapping("/updateBankInfo")
    public Result<Boolean> updateBankInfo(@ApiIgnore @LoginUser AppLoginUser appLoginUser,
                                          @RequestBody AgentUpdateBankInfoDto agentUpdateBankInfoDto,
                                          HttpServletRequest request) {
        String mobile = "";
        UserInfo userInfo = appLoginUser.getUserInfo();
        if (userInfo != null) {
            mobile = userInfo.getMobile();
        }
        agentUpdateBankInfoDto.setUserId(appLoginUser.getUserId());
        agentUpdateBankInfoDto.setMobile(mobile);
        agentUpdateBankInfoDto.setApplyIp(IpUtil.getIpAddr(request));
        return agentEnterApplyApi.updateBankInfo(agentUpdateBankInfoDto);
    }

}
